﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using StockStar.GatherData;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;

namespace Report
{
    class GatherData
    {
        public void DoWork()
        {
            UpJyts();
            UpShJygkxx();
            //upFuture();
            //UpForex();

            Console.WriteLine("采集限售解禁股！");
            StockBan sb = new StockBan();
            sb.Import();
            Console.WriteLine("采集大宗交易信息！");
            BlockTrade bt = new BlockTrade();
            bt.Import();

            CreateKpzzd();
            CreateKpzzdByDate();
            ClearCache();
            //Console.Read();
        }

        #region 抓取交易提示
        /// <summary>
        /// 抓取交易提示
        /// </summary>
        private static void UpJyts()
        {
            #region 抓取上证所数据
            string _url = "http://www.sse.com.cn/sseportal/webapp/datapresent/querytradetipsnew";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            Match mcTitle = Regex.Match(_Content, "<td class=\"Title_l\" valign=\"top\" width=\"100%\">(?<g1>[\\d]{4}\\-[\\d]{2}\\-[\\d]{2})[^<]*</td>");
            string title = mcTitle.Groups["g1"].Value;
            StringBuilder sbsql = new StringBuilder();
            if (!chkTitle(title, 1))
            {
                MatchCollection mcs = Regex.Matches(_Content, "<tr bgcolor=\"#F4F4F4\" height=\"20\">([^<]+(<td[^>]+>[^<]+</td>)){5}([^<]+</tr>)");
                foreach (Match mc in mcs)
                {
                    string mcContent = mc.Value;
                    MatchCollection tmcs = Regex.Matches(mcContent, "<td class=\"table3\" bgcolor=\"((#dbedf8)|(white))\" nowrap>[^<]+</td>");
                    string gpdm = Regex.Replace(tmcs[0].Value, "<[^>]+>", "", RegexOptions.IgnoreCase);
                    string gpjc = Regex.Replace(tmcs[1].Value, "<[^>]+>", "", RegexOptions.IgnoreCase);
                    string tpsj = Regex.Replace(tmcs[2].Value, "<[^>]+>", "", RegexOptions.IgnoreCase);
                    string tpqx = Regex.Replace(tmcs[3].Value, "<[^>]+>", "", RegexOptions.IgnoreCase);
                    string tpyy = Regex.Replace(tmcs[4].Value, "<[^>]+>", "", RegexOptions.IgnoreCase);
                    sbsql.AppendFormat("insert into kp_jyts (gpdm,gpjc,tpsj,tpqx,tpyy,source,title) values ('{0}','{1}','{2}','{3}','{4}',1,'{5}')\r\n", gpdm, gpjc, tpsj, tpqx, tpyy, title);
                }
            }
            else
            {
                Console.WriteLine("上交所交易提示无更新!");
            }
            #endregion

            #region 抓取深交所数据
            _url = "http://www.szse.cn/main/disclosure/news/tfpts/";
            _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            mcTitle = Regex.Match(_Content, "<span class='cls-subtitle'>(?<g1>[\\d]{4}\\-[\\d]{2}\\-[\\d]{2})</span>");
            title = mcTitle.Groups["g1"].Value;
            if (!chkTitle(title, 2))
            {
                MatchCollection mcs = Regex.Matches(_Content, "<tr  class='cls-data-tr'[^>]+>(<td[^>]*>[^<]*</td>){6}</tr>");
                foreach (Match mc in mcs)
                {
                    string xml = mc.Value;
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);
                    XmlNodeList nodes = doc.GetElementsByTagName("td");
                    string gpdm = nodes[0].InnerText.Trim();
                    string gpjc = nodes[1].InnerText.Trim();
                    string tpsj = nodes[2].InnerText.Trim();
                    //string gbrq = nodes[0].InnerText;
                    string fpsj = nodes[3].InnerText.Trim();
                    string tpqx = nodes[4].InnerText.Trim();
                    string tpyy = nodes[5].InnerText.Trim();

                    sbsql.AppendFormat("insert into kp_jyts (gpdm,gpjc,tpsj,tpqx,fpsj,tpyy,source,title) values ('{0}','{1}','{2}','{3}','{4}','{5}',2,'{6}')\r\n", gpdm, gpjc, tpsj, tpqx, fpsj, tpyy, title);
                }
            }
            else
            {
                Console.WriteLine("深交所交易提示无更新!");
            }
            #endregion

            string sql = sbsql.ToString();

            #region 执行SQL
            if (!string.IsNullOrEmpty(sbsql.ToString()))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sbsql.ToString(), null);
                            trans.Commit();
                            Console.WriteLine("交易提示更新完成!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("交易提示更新异常:" + ex.Message);
                            LogFile.WriteLog("交易提示更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
        /// <summary>
        /// 交易提示是否已更新
        /// </summary>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static bool chkTitle(string title, int type)
        {
            string sql = string.Format("select id from kp_jyts where title='{0}' and source={1}", title, type);
            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.StrConn, CommandType.Text, sql, null);
            int id = 0;
            if (reader.Read())
            {
                id = int.Parse(reader["id"].ToString());
            }
            if (id > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 交易公开信息
        /// <summary>
        /// 获取上交所交易公开信息
        /// </summary>
        private static void UpShJygkxx()
        {
            #region 上交所
            string _url = "http://www.sse.com.cn/sseportal/webapp/datapresent/SSENewTradeInfoPublishAct";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            Match mc = Regex.Match(_Content, "<td class=\"content_gkjy\" valign=\"top\" width=\"100%\">(?<g1>[\\s\\S]*)</td>");
            string content = mc.Groups["g1"].Value.Split(new string[] { "</td>" }, StringSplitOptions.None)[0];
            content = content.Replace("\r\n\r\n\r\n", "");
            Match mcdate = Regex.Match(content, "(?<year>[\\d]{4})年(?<mouth>[\\d]{2})月(?<day>[\\d]{2})日");
            string date = string.Format("{0}-{1}-{2}", mcdate.Groups["year"].Value, mcdate.Groups["mouth"].Value, mcdate.Groups["day"].Value);
            string sql = "";
            if (!chkJygkxx(date, 1))
            {
                sql = string.Format("insert into kp_gkxx (xxrq,content,source) values ('{0}','{1}',1)\r\n", date, content);
            }
            else
            {
                Console.WriteLine("上交所交易公开信息无更新!");
            }
            #endregion

            DateTime dt = DateTime.Now.AddDays(-1);
            _url = string.Format("http://www.szse.cn/szseWeb/common/szse/files/text/jy/jy{0}.txt", dt.ToString("yyMMdd"));
            _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            if (!string.IsNullOrEmpty(_Content) && !chkJygkxx(dt.ToString("yyyy-MM-dd"), 2))
            {
                sql += string.Format("insert into kp_gkxx (xxrq,content,source) values ('{0}','{1}',2)\r\n", dt.ToString("yyyy-MM-dd"), _Content);
            }
            else
            {
                Console.WriteLine("深交所交易公开信息无更新!");
            }


            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("交易公开信息更新完成!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("交易公开信息更新异常:" + ex.Message);
                            LogFile.WriteLog("交易公开信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }

        private static bool chkJygkxx(string date, int type)
        {
            string sql = string.Format("select id from kp_gkxx where xxrq='{0}' and source={1}", date, type);
            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.StrConn, CommandType.Text, sql, null);
            int id = 0;
            if (reader.Read())
            {
                id = int.Parse(reader["id"].ToString());
            }
            if (id > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 采集期货信息
        public static void upFuture()
        {
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONH0,HONH0,GLNJ0,SLNH0,CHCH0,SBCH0,WHCH0,CRCH0,SMCH0,SOCH0,CTNH0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            //string _url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=CONK0,HONJ0,GLNJ0,SLNN0,CHCK0,SBCK0,WHCK0,CRCK0,SMCK0,SOCK0,CTNK0,LCPS0,LALS0,LZNS0,LTNS0,LNKS0,LLDS0";
            string _url = GetQihuoUrl();
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            _Content = _Content.Replace("var js=", "");
            //var re = Newtonsoft.Json.JsonConvert.DeserializeObject(_Content);
            MatchCollection mcs = Regex.Matches(_Content, "\"(?<g1>[^\"]*)\"");
            StringBuilder sqlb = new StringBuilder();
            foreach (Match mc in mcs)
            {
                string value = mc.Value;
                string[] args = value.Split(new char[] { ',' });
                switch (args[0].Replace("\"", "").Substring(0, 3))
                {
                    case "CON":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='轻质原油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "HON":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='燃料油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "GLN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='黄金'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SLN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='白银'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    //case "CHCH0":
                    //    sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}' where name='轻质原油'\r\n", args[5], args[18]);
                    //    break;
                    case "SBC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='大豆'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "WHC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='小麦'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CRC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='玉米'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SMC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='豆粕'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "SOC":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='豆油'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "CTN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='棉花'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LCP":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月铜'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LAL":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月铝'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LZN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月锌'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LTN":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月锡'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LNK":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月镍'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    case "LLD":
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}',insertdata='{2}' where name='3月铅'\r\n", args[5], args[18], DateTime.Now.ToString());
                        break;
                    default:
                        break;
                }
            }

            _url = "http://bdcjhq.hexun.com/quote?s2=SUGAR.nme,undefined";
            _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            Match mc1 = Regex.Match(_Content, "la:\"(?<g1>[^\"]*)\"");
            Match mc2 = Regex.Match(_Content, "zdf:\"(?<g1>[^\"]*)\"");
            sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where name='白糖'\r\n", mc1.Groups["g1"].Value, mc2.Groups["g1"].Value);
            string sql = sqlb.ToString();
            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("期货信息更新成功!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("期货信息更新异常:" + ex.Message);
                            LogFile.WriteLog("期货信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
        #endregion

        #region 采集外汇数据
        private void UpForex() 
        {
            string _url = "http://qt.gtimg.cn/&r=0.22351773461386642q=EUR,JPY,AUD,USDCNY";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");
            MatchCollection mcs = Regex.Matches(_Content, "\"(?<g1>[^\"]*)\"");
            StringBuilder sqlb = new StringBuilder();
            int i = 0;
            foreach (Match mc in mcs)
            {
                i++;
                string[] args = mc.Value.Replace("\"", "").Split(new char[] { ',' });
                float price = float.Parse(args[1]);
                float preclose = float.Parse(args[5]);
                float zdf = ((price - preclose) / price) * 100;
                switch (i) 
                {
                    case 1:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=22", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 2:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=20", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 3:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=23", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    case 4:
                        sqlb.AppendFormat("update ZS_ContentData set newprice='{0}',ZDF='{1}%' where id=21", price.ToString("0.0000"), zdf.ToString("0.0000"));
                        break;
                    default:
                        break;
                }
            }

            string sql = sqlb.ToString();
            #region 执行SQL
            if (!string.IsNullOrEmpty(sql))
            {
                using (SqlConnection conn = new SqlConnection(SQLHelper.StrConn))
                {
                    conn.Open();
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        try
                        {
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                            trans.Commit();
                            Console.WriteLine("外汇信息更新成功!");
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            Console.WriteLine("外汇信息更新异常:" + ex.Message);
                            LogFile.WriteLog("外汇信息更新异常:" + ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
        #endregion

        public static void CreateKpzzd() 
        {
            Dictionary<string, string> _url = new Dictionary<string, string>();
            _url.Add("kpts.htm", "http://cms.emoney.cn/api/kpts.ashx");
            _url.Add("gkxx_sh.htm","http://cms.emoney.cn/api/gkxx.ashx?source=1");
            _url.Add("gkxx_sz.htm", "http://cms.emoney.cn/api/gkxx.ashx?source=2");
            _url.Add("xsjj.htm", "http://cms.emoney.cn/api/xsjj.ashx");
            _url.Add("dzjy.htm", "http://cms.emoney.cn/api/dzjy.ashx");

            string dir = "D:\\down\\kpzzd\\";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            foreach (string key in _url.Keys) 
            {
                string turl = _url[key];
                string _Content = OperMethod.GetPageByUrl(turl, "utf-8");
                string _path = dir + key;
                Console.WriteLine("自在生成:" + turl);
                using (StreamWriter sw = new StreamWriter(_path, false, Encoding.UTF8))
                {
                    sw.Write(_Content);
                    sw.Close();
                }
            }
        }

        public static void CreateKpzzdByDate()
        {            
            for (int i = 0; i >= -30; i--)
            {
                Dictionary<string, string> _url = new Dictionary<string, string>();
                string title = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                _url.Add("kpts.htm", "http://cms.emoney.cn/api/kpts.ashx?date="+title);
                _url.Add("gkxx_sh.htm", "http://cms.emoney.cn/api/gkxx.ashx?source=1&date=" + title);
                _url.Add("gkxx_sz.htm", "http://cms.emoney.cn/api/gkxx.ashx?source=2&date=" + title);
                _url.Add("xsjj.htm", "http://cms.emoney.cn/api/xsjj.ashx?date=" + title);
                _url.Add("dzjy.htm", "http://cms.emoney.cn/api/dzjy.ashx?date=" + title);

                string dir = "D:\\down\\kpzzd\\" + DateTime.Now.AddDays(i).ToString("yyyyMMdd") + "\\";
                if (!Directory.Exists(dir)) 
                {
                    Directory.CreateDirectory(dir);
                }

                foreach (string key in _url.Keys)
                {
                    string turl = _url[key];
                    string _Content = OperMethod.GetPageByUrl(turl, "utf-8");
                    string _path = dir + key;
                    Console.WriteLine("自在生成:" + turl);
                    using (StreamWriter sw = new StreamWriter(_path, false, Encoding.UTF8))
                    {
                        sw.Write(_Content);
                        sw.Close();
                    }
                }
            }
        }

        public static void ClearCache() 
        {
            Console.WriteLine("清除开般早知道！");
            string _url = "http://ccms.chinacache.com/index.jsp?user=emstock&pswd=Emstock123&ok=ok&dirs=http://down3.emstock.com.cn/kpzzd/";
            string _content = OperMethod.GetPageByUrl(_url, "Gb2312");
            Console.WriteLine("清除缓存完成！");
        }

        /// <summary>
        /// 获取东财外汇数据链接
        /// </summary>
        /// <returns>返回链接</returns>
        public static string GetQihuoUrl() 
        {
            string _url = "http://hqres.eastmoney.com/EMQuote_Qihuo/js/quote_qihuo.js";
            string _Content = OperMethod.GetPageByUrl(_url, "Gb2312");

            MatchCollection mcs = Regex.Matches(_Content, "(?<g1>[A-Z]{4}0)\\|");

            StringBuilder argsb = new StringBuilder();
            
            foreach (Match mc in mcs) 
            {
                argsb.AppendLine(mc.Groups["g1"].Value);
            }

            string args = argsb.ToString().Replace("\r\n", ",");
            string url = "http://hq2gjqh.eastmoney.com/EM_Futures2010NumericApplication/index.aspx?type=z&ids=" + args.Substring(0, args.Length - 1);
            return url;
        }

    }
}
